/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.oracleandsxnd.erp.xsd.service;

import com.oracleandsxnd.erp.common.persistence.Page;
import com.oracleandsxnd.erp.common.service.CrudService;
import com.oracleandsxnd.erp.common.utils.BeanUtils;
import com.oracleandsxnd.erp.common.utils.StringUtils;
import com.oracleandsxnd.erp.xsd.dao.XsglSaleChangeDao;
import com.oracleandsxnd.erp.xsd.dao.XsglSaleHDetailDao;
import com.oracleandsxnd.erp.xsd.dao.XsglSaleTDetailDao;
import com.oracleandsxnd.erp.xsd.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 换货单Service
 * @author 换货单
 * @version 2019-12-21
 */
@Service
@Transactional(readOnly = true)
public class XsglSaleChangeService extends CrudService<XsglSaleChangeDao, XsglSaleChange> {

	@Autowired
	private XsglSaleHDetailDao xsglSaleHDetailDao;
	@Autowired
	private XsglSaleTDetailDao xsglSaleTDetailDao;
	
	public XsglSaleChange get(String id) {
		XsglSaleChange xsglSaleChange = super.get(id);
		xsglSaleChange.setXsglSaleHDetailList(xsglSaleHDetailDao.findList(new XsglSaleHDetail(xsglSaleChange)));
		xsglSaleChange.setXsglSaleTDetailList(xsglSaleTDetailDao.findList(new XsglSaleTDetail(xsglSaleChange)));
		return xsglSaleChange;
	}
	
	public List<XsglSaleChange> findList(XsglSaleChange xsglSaleChange) {
		return super.findList(xsglSaleChange);
	}
	
	public Page<XsglSaleChange> findPage(Page<XsglSaleChange> page, XsglSaleChange xsglSaleChange) {
		return super.findPage(page, xsglSaleChange);
	}

	@Autowired
	private XsglSaleService xsglSaleService;
	@Transactional(readOnly = false)
	public void save(XsglSaleChange xsglSaleChange) {
		super.save(xsglSaleChange);
		for (XsglSaleHDetail xsglSaleHDetail : xsglSaleChange.getXsglSaleHDetailList()){
			if (xsglSaleHDetail.getId() == null){
				continue;
			}
			if (XsglSaleHDetail.DEL_FLAG_NORMAL.equals(xsglSaleHDetail.getDelFlag())){
				if (StringUtils.isBlank(xsglSaleHDetail.getId())){
					xsglSaleHDetail.setSale(xsglSaleChange);
					xsglSaleHDetail.preInsert();
					xsglSaleHDetailDao.insert(xsglSaleHDetail);
				}else{
					xsglSaleHDetail.preUpdate();
					xsglSaleHDetailDao.update(xsglSaleHDetail);
				}
			}else{
				xsglSaleHDetailDao.delete(xsglSaleHDetail);
			}
		}
		for (XsglSaleTDetail xsglSaleTDetail : xsglSaleChange.getXsglSaleTDetailList()){
			if (xsglSaleTDetail.getId() == null){
				continue;
			}
			if (XsglSaleTDetail.DEL_FLAG_NORMAL.equals(xsglSaleTDetail.getDelFlag())){
				if (StringUtils.isBlank(xsglSaleTDetail.getId())){
					xsglSaleTDetail.setSale(xsglSaleChange);
					xsglSaleTDetail.preInsert();
					xsglSaleTDetailDao.insert(xsglSaleTDetail);
				}else{
					xsglSaleTDetail.preUpdate();
					xsglSaleTDetailDao.update(xsglSaleTDetail);
				}
			}else{
				xsglSaleTDetailDao.delete(xsglSaleTDetail);
			}
		}


		//生成退货单
		XsglSale txsglSale=new XsglSale();
		BeanUtils.copyBeanProp(txsglSale,xsglSaleChange);
		txsglSale.setType("2");
		List<XsglSaleDetail> tds=new ArrayList<XsglSaleDetail>();
		for (XsglSaleTDetail xsglSaleHDetail : xsglSaleChange.getXsglSaleTDetailList()){
			if (xsglSaleHDetail.getId() == null){
				continue;
			}
			if (XsglSaleHDetail.DEL_FLAG_NORMAL.equals(xsglSaleHDetail.getDelFlag())){
				XsglSaleDetail xsd=new XsglSaleDetail();
				BeanUtils.copyBeanProp(xsd,xsglSaleHDetail);
				xsd.setId(null);
				tds.add(xsd);
			}
		}
		txsglSale.setXsglSaleDetailList(tds);
		txsglSale.setId(null);

		xsglSaleService.save(txsglSale);
		//生成销售单
		XsglSale hxsglSale=new XsglSale();
		BeanUtils.copyBeanProp(hxsglSale,xsglSaleChange);

        List<XsglSaleDetail> hds=new ArrayList<XsglSaleDetail>();
		for (XsglSaleHDetail xsglSaleHDetail : xsglSaleChange.getXsglSaleHDetailList()){
			if (xsglSaleHDetail.getId() == null){
				continue;
			}
			if (XsglSaleHDetail.DEL_FLAG_NORMAL.equals(xsglSaleHDetail.getDelFlag())){
				XsglSaleDetail xsd=new XsglSaleDetail();
				BeanUtils.copyBeanProp(xsd,xsglSaleHDetail);
				xsd.setId(null);
				hds.add(xsd);
			}
		}
		hxsglSale.setType("1");
		hxsglSale.setXsglSaleDetailList(hds);
		hxsglSale.setId(null);
		xsglSaleService.save(hxsglSale);

	}
	
	@Transactional(readOnly = false)
	public void delete(XsglSaleChange xsglSaleChange) {
		super.delete(xsglSaleChange);
		xsglSaleHDetailDao.delete(new XsglSaleHDetail(xsglSaleChange));
		xsglSaleTDetailDao.delete(new XsglSaleTDetail(xsglSaleChange));
	}
	
}